package com.fy56.platform.logistics.service.impl;

import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.mapper.ReceiverContactInfoMapper;
import com.fy56.platform.logistics.model.ReceiverContactInfo;
import com.fy56.platform.logistics.service.wx.IReceiverContactInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 收货联系人Service实现
 * @author li
 * @date 2018-05-30 16:58
 */
@Service
public class ReceiverContactInfoServiceImpl implements IReceiverContactInfoService {

    @Autowired
    private ReceiverContactInfoMapper receiverContactInfoMapper;

    /**
     * 根据用户ID获取收货联系人
     *
     * @param vipMemberId
     * @return
     */
    @Override
    public List<ReceiverContactInfo> selectByVipMemberId(Long vipMemberId){
        return receiverContactInfoMapper.selectByVipMemberId(vipMemberId);
    }

    @Override
    public List<ReceiverContactInfo> selectByVipMemberIdandName(ReceiverContactInfo record){
        return receiverContactInfoMapper.selectByVipMemberIdandName(record);
    }
    @Override
    public ReceiverContactInfo getExistsByVipMemberIdAndMobile(ReceiverContactInfo record){
        return receiverContactInfoMapper.getExistsByVipMemberIdAndMobile(record);
    }

    @Override
    public ReceiverContactInfo selectByPrimaryKey(Long id) {
        return receiverContactInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public ReceiverContactInfo get(ReceiverContactInfo entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel insert(ReceiverContactInfo entity) {
        ResultModel result = null;
        if(entity.getIsDefault() == 1){
            //更新所有收货联系人记录的是否默认为 0
            receiverContactInfoMapper.updateDefaultFlag(entity.getVipMemberId());
        }
        int r = receiverContactInfoMapper.insert(entity);
        if(r == 0) {
            result = ResultModel.error(ResultStatus.INSERT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = true)
    public ResultModel insertSelective(ReceiverContactInfo entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel update(ReceiverContactInfo entity) {
        ResultModel result = null;
        if(entity.getIsDefault() == 1){
            //更新所有收货联系人记录的是否默认为 0
            receiverContactInfoMapper.updateDefaultFlag(entity.getVipMemberId());
        }
        int r = receiverContactInfoMapper.updateByPrimaryKey(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updateSelective(ReceiverContactInfo entity) {
        ResultModel result = null;
        int r = receiverContactInfoMapper.updateByPrimaryKeySelective(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(Long id) {
        int r = receiverContactInfoMapper.deleteByPrimaryKey(id);
        if (r == 0){
            return new ResultModel(ResultStatus.DELETE_ERR);
        }
        return ResultModel.ok();
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(ReceiverContactInfo entity) {
        int r = receiverContactInfoMapper.updateByPrimaryKeySelective(entity);
        if (r == 0){
            return new ResultModel(ResultStatus.DELETE_ERR);
        }
        return ResultModel.ok();
    }

    @Override
    public List<ReceiverContactInfo> findList(ReceiverContactInfo entity) {
        List<ReceiverContactInfo> list = receiverContactInfoMapper.findList(entity);
        return list;
    }

}
